package com.swss.g.dept.service.impl;

import java.util.List;

import net.sf.json.JSONArray;

import com.swss.g.base.impl.BaseServiceImpl;
import com.swss.g.dept.bean.Dept;
import com.swss.g.dept.dao.DeptDAO;
import com.swss.g.dept.service.DeptService;
import com.swss.util.HqlUtil;
import com.swss.util.Pagination;

public class DeptServiceImpl extends BaseServiceImpl<Dept> implements
		DeptService {

	private DeptDAO deptDao;

	public DeptDAO getDeptDao() {
		return deptDao;
	}

	public void setDeptDao(DeptDAO deptDao) {
		this.setBaseDao(deptDao);
		this.deptDao = deptDao;
	}

	public void deleteDeptInfo(Dept dept) throws Exception {
		// TODO Auto-generated method stub
		String deptNo = dept.getDeptNo();// 单位编号
		int deptNoLen = deptNo.trim().length();// 编号长度
		String deptNoHigh = "";
		String hql = "";
		try {
			if (!deptNo.endsWith("00")) {
				hql = "FROM Dept WHERE deptNo LIKE '" + deptNo
						+ "%' ORDER BY deptNo ASC";
				List<Dept> deptList = this.getDeptDao().findByHql(hql); // 获取本单位以及下级单位
				this.getDeptDao().deleteAll(deptList); // 删除此单位和下级单位
				/***************************************************************
				 * 预留删除 人员和存放地点内容
				 * 
				 */
			}
			if (deptNoLen != 2) {
				deptNoHigh = deptNo.substring(0, deptNoLen - 2); // 获取上级单位编号
				hql = "FROM Dept WHERE deptNo LIKE '" + deptNoHigh
						+ "%' ORDER BY deptNo ASC";

				// 判断上级单位 和同级系列单位的 数量为1 则表示上级单位应该被改为末级
				if (this.getDeptDao().findByHql(hql).size() == 1) {
					Dept deptUpdate = this.findOneDept(deptNoHigh);
					deptUpdate.setDeptFlag("*");
					this.getDeptDao().update(deptUpdate);// 更新上级单位的 *标记
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public Dept findOneDept(String deptNo) throws Exception {
		// TODO Auto-generated method stub
		String hql = "FROM Dept WHERE deptNo='" + deptNo.trim() + "'";
		return this.getDeptDao().findOneByHql(hql);
	}

	public int insertDeptInfo(Dept dept) throws Exception {
		// TODO Auto-generated method stub
		String hql = "";
		dept.setFlag("2");
		String deptNo = dept.getDeptNo();// 单位编号
		String deptNoHigh = "";// 上级单位编号
		int deptNoLen = deptNo.trim().length();// 编号长度

		if (this.findOneDept(deptNo) == null) {// 判断重复

			for (int i = 4; i <= 10; i = i + 2) {
				if (deptNoLen == i) {// 判断单位编号长度4-10之前的
					deptNoHigh = deptNo.substring(0, i - 2);
					hql = "FROM Dept WHERE deptNo LIKE '" + deptNoHigh
							+ "%' ORDER BY deptNo ASC";
					if (this.deptDao.findByHql(hql).size() == 0) {// 判断有无上级单位
						return 2;// 无上级单位
					} else {// 有上级单位
						Dept deptHigh = this.findOneDept(deptNoHigh);
						deptHigh.setDeptFlag("");
						this.deptDao.update(deptHigh);// 更新上级单位的 *标记
					}
				}
			}
			dept.setDeptFlag("*");
			this.getDeptDao().save(dept);// 插入新单位信息
			return 1;// 成功
		} else {
			return 3; // 编号重复
		}
	}

	@SuppressWarnings("unchecked")
	public Pagination<Dept> findDeptByLike(Pagination pagination, Dept dept,
			String deptNo) throws Exception {
		// TODO Auto-generated method stub
		if (pagination == null) {
			pagination = new Pagination();

		}
		String hql = "";
		hql = "FROM Dept d WHERE 1=1 ";
		if (!deptNo.equals("00")) {
			hql += " AND d.deptNo LIKE '" + deptNo.trim() + "%'";
		}
		List deptList = this.getDeptDao().findByHql(
				hql + HqlUtil.bean2HqlLike(dept, "d") + " ORDER BY d."
						+ pagination.getOrderBy() + " "
						+ pagination.getOrderType(), pagination);
		pagination.setResult(deptList);
		pagination.setRowcount(this.deptDao.findCount(hql));
		return pagination;
	}

	public List<Dept> findDeptTreeByNo(String deptNo) throws Exception {
		// TODO Auto-generated method stub

		if (deptNo.equals("00")) {
			return this.deptDao.findDeptByNoLen(2);
		} else {
			return this.deptDao.findDeptChildrenByNo(deptNo);
		}

	}

	public String findDeptTreeJSONByNo(String deptNo) throws Exception {
		// TODO Auto-generated method stub
		List deptList = null;
		if (deptNo.equals("00")) {
			deptList = this.deptDao.findDeptByNoLen(2);
		} else {
			deptList = this.deptDao.findDeptChildrenByNo(deptNo);
		}
		// JSONObject.fromObject(deptList).toString();
	
		return JSONArray.fromObject(deptList).toString();
	}
}
